package com.tsl.prevention.modules.life.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsl.prevention.modules.life.entity.ServiceLife;
import com.tsl.prevention.modules.life.service.IServiceLifeService;
import com.tsl.prevention.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lionel
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/life/service-life")
public class ServiceLifeController {
    @Autowired
    private IServiceLifeService iServiceLifeService;

    @GetMapping("/getList")
    public Result getList(Integer state,String serviceName) {
        QueryWrapper<ServiceLife> queryWrapper = new QueryWrapper<>();
        if (state != null) {
            queryWrapper.eq("state",state);
        }
        if (serviceName != null && !serviceName.equals("undefined")) {
            queryWrapper.like("service_name","%"+serviceName+"%");
        }
        List<ServiceLife> list = iServiceLifeService.list(queryWrapper.orderByAsc("createtime"));
        return Result.create(list);
    }

    @GetMapping("/getService")
    public Result getService(String id) {
        ServiceLife byId = iServiceLifeService.getById(id);
        return Result.create(byId);
    }

    @PostMapping("/updateService")
    public Result updateService(String id, Integer state) {
        ServiceLife serviceLife = new ServiceLife();
        serviceLife.setId(id);
        serviceLife.setState(state);
        iServiceLifeService.updateById(serviceLife);
        return Result.create();
    }

    @PostMapping("/addService")
    public Result addService(@RequestBody ServiceLife serviceLife) {
        iServiceLifeService.save(serviceLife);
        return Result.create();
    }

    @GetMapping("/delService")
    public Result delService(String id) {
        iServiceLifeService.removeById(id);
        return Result.create();
    }
}

